<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <title>
        Java 大数字运算 |
        
        Darklovy</title>
    <meta name="description" content>
    <meta name="keywords" content>
    <meta name="HandheldFriendly" content="True">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link rel="shortcut icon" href="/">
    <link rel="alternate" href="/atom.xml" title="Darklovy">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="在 java中提供了大数字的操作类，即 Java.math.BigInteger 类与 java.math.BigDecimal 类。这两个类用于高精度计算，其中BigInteger 类是针对大整数的处理，而BigDecimal 类则是针对大小数的处理类。 BigIntegerBigInteger 类型的数字范围较 Integer 类型的数字范围要大得多。BigInteger 支持任意精度的整数。">
<meta name="keywords" content="Java">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 大数字运算">
<meta property="og:url" content="https://www.ttxxly.top/2019/01/15/Java-大数字运算/index.html">
<meta property="og:site_name" content="Darklovy">
<meta property="og:description" content="在 java中提供了大数字的操作类，即 Java.math.BigInteger 类与 java.math.BigDecimal 类。这两个类用于高精度计算，其中BigInteger 类是针对大整数的处理，而BigDecimal 类则是针对大小数的处理类。 BigIntegerBigInteger 类型的数字范围较 Integer 类型的数字范围要大得多。BigInteger 支持任意精度的整数。">
<meta property="og:locale" content="en">
<meta property="og:updated_time" content="2019-02-18T02:56:53.450Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java 大数字运算">
<meta name="twitter:description" content="在 java中提供了大数字的操作类，即 Java.math.BigInteger 类与 java.math.BigDecimal 类。这两个类用于高精度计算，其中BigInteger 类是针对大整数的处理，而BigDecimal 类则是针对大小数的处理类。 BigIntegerBigInteger 类型的数字范围较 Integer 类型的数字范围要大得多。BigInteger 支持任意精度的整数。">

    <link href="https://fonts.googleapis.com/css?family=Inconsolata|Titillium+Web" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
    <link href="//cdn.bootcss.com/node-waves/0.7.5/waves.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/style.css">
    <script>
        function setLoadingBarProgress(num) {
            document.getElementById('loading-bar').style.width = num + "%";
        }
    </script>
</head>
</html>
<body>
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>


  <script>setLoadingBarProgress(20)</script> 
  <header class="l_header">
	<div class='wrapper'>
		<div class="nav-main container container--flex">
			<a class="logo flat-box" href='/' >
				Darklovy
			</a>
			<div class='menu'>
				<ul class='h-list'>
					
						<li>
							<a class='flat-box nav-Home' href='/'>
								首页
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-archives' href='/archives'>
								归档
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-categories' href='/categories'>
								分类
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-tags' href='/tags'>
								标签
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-algorithm' href='https://book.algorithm.ttxxly.top/'>
								算法
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-python' href='https://book.python.ttxxly.top/'>
								python
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-about' href='/about'>
								关于
							</a>
						</li>
					
				</ul>
				<div class='underline'></div>
			</div>
			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="Search" />
						<span class="icon icon-search"></span>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a href='javascript:void(0)'><span class="icon icon-search flat-box"></span></a></li>
				
				<li class='s-menu'><a href='javascript:void(0)'><span class="icon icon-menu flat-box"></span></a></li>
			</ul>
		</div>
		
		<div class='nav-sub container container--flex'>
			<a class="logo" class="flat-box" href='javascript:void(0)'>
				Word of Forks
			</a>

			<ul class='switcher h-list'>
				<li class='s-comment'><a href='javascript:void(0)'><span class="icon icon-chat_bubble_outline flat-box"></span></a></li>
				<li class='s-top'><a href='javascript:void(0)'><span class="icon icon-arrow_upward flat-box"></span></a></li>
				<li class='s-toc'><a href='javascript:void(0)'><span class="icon icon-format_list_numbered flat-box"></span></a></li>
			</ul>
		</div>
	</div>
</header>
<aside class="menu-phone">
	<nav>
		
			<a href="/" class="nav-Home nav">
				首页
			</a>
		
			<a href="/archives" class="nav-archives nav">
				归档
			</a>
		
			<a href="/categories" class="nav-categories nav">
				分类
			</a>
		
			<a href="/tags" class="nav-tags nav">
				标签
			</a>
		
			<a href="https://book.algorithm.ttxxly.top/" class="nav-algorithm nav">
				算法
			</a>
		
			<a href="https://book.python.ttxxly.top/" class="nav-python nav">
				python
			</a>
		
			<a href="/about" class="nav-about nav">
				关于
			</a>
		
	</nav>
</aside>

    <script>setLoadingBarProgress(40);</script>
  <div class="l_body">
    <div class='container clearfix'>
      <div class='l_main'>
        <article id="post-Java-大数字运算"
  class="post white-box article-type-post"
  itemscope itemprop="blogPost">
	<section class='meta'>
	<h2 class="title">
  	<a href="/2019/01/15/Java-大数字运算/">
    	Java 大数字运算
    </a>
  </h2>
	<time>
	  Jan 15, 2019
	</time>
	
	</section>
	
		<section class="toc-wrapper"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#BigInteger"><span class="toc-number">1.</span> <span class="toc-text">BigInteger</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#BigDecimal"><span class="toc-number">2.</span> <span class="toc-text">BigDecimal</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#两个常用的构造方法"><span class="toc-number">2.1.</span> <span class="toc-text">两个常用的构造方法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#面试题"><span class="toc-number">3.</span> <span class="toc-text">面试题</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#下列程序的输出结果是什么？"><span class="toc-number">3.1.</span> <span class="toc-text">下列程序的输出结果是什么？</span></a></li></ol></li></ol></section>
	
	<section class="article typo">
  	<div class="article-entry" itemprop="articleBody">
    	<p>在 java中提供了大数字的操作类，即 Java.math.BigInteger 类与 java.math.BigDecimal 类。这两个类用于高精度计算，其中BigInteger 类是针对大整数的处理，而BigDecimal 类则是针对大小数的处理类。</p>
<h3 id="BigInteger"><a href="#BigInteger" class="headerlink" title="BigInteger"></a>BigInteger</h3><p>BigInteger 类型的数字范围较 Integer 类型的数字范围要大得多。BigInteger 支持任意精度的整数。</p>
<p>在 BigInteger 类中封装了多种操作，除了基本的加、减、乘、除操作之外，还提供了绝对值、相反数、最大公约数以及判断是否为质数等操作。</p>
<p>使用 BigInteger 类，可以实例化一个 BigInteger 对象，彬自动调用相应的构造函数。BigInteger 类具有很多构造函数，但最直接的一种是参数以字符串形式代表要处理的数字。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">BigInteger</span><span class="params">(String val)</span></span>;</span><br><span class="line"></span><br><span class="line">val 是十进制数字符串</span><br><span class="line"></span><br><span class="line">BigInteger twoInstance = <span class="keyword">new</span> BigInteger(<span class="string">"2"</span>);</span><br></pre></td></tr></table></figure>
<p>下面列举了 BigInteger 类中常用的几种运算方法<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">add</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做加法运算</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">subftract</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做减法运算</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">multiply</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做乘法运算</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">divide</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做除法运算</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">remainder</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做取余运算</span></span><br><span class="line"><span class="keyword">public</span> BigInteger[] divideAndReainder(BigInteger val);<span class="comment">//用数组返回余数和商，结果数组中第一个值为商，第二个值为余数</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">pow</span><span class="params">(<span class="keyword">int</span> exponent)</span></span>;<span class="comment">//取参数的 exponent 次方操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">negate</span><span class="params">()</span></span>;<span class="comment">//取相反数</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">shiftLeft</span><span class="params">(<span class="keyword">int</span> n)</span></span>;<span class="comment">//将数字左移 n 位，如果 n 为负数，做右移操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">shiftRight</span><span class="params">(<span class="keyword">int</span> n)</span></span>;<span class="comment">//将数字右移 n 位，如果 n 为负数，做左移操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">and</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//与操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">or</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//或操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compareTo</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//数字比较操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object x)</span></span>;<span class="comment">//当参数x是BigInteger类型的珠子并且数值相等时，返回true</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">min</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//返回较小的数字</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">max</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//返回较大的数字</span></span><br></pre></td></tr></table></figure></p>
<p><strong>例子：实现大数的各种运算操作</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.math.BigInteger;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BigIntegerDemo</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        BigInteger bigInstance = <span class="keyword">new</span> BigInteger(<span class="string">"4"</span>);、、实例化一个大数字</span><br><span class="line">        <span class="comment">//取该大数字 加 2 的操作</span></span><br><span class="line">        System.out.println(<span class="string">"加法操作："</span>+bigInstance.add(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>)));</span><br><span class="line">         <span class="comment">//取该大数字 减 2 的操作</span></span><br><span class="line">        System.out.println(<span class="string">"减法操作："</span>+bigInstance.subtract(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>)));</span><br><span class="line">         <span class="comment">//取该大数字 乘 2 的操作</span></span><br><span class="line">        System.out.println(<span class="string">"乘法操作："</span>+bigInstance.multiply(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>)));</span><br><span class="line">         <span class="comment">//取该大数字 除 2 的操作</span></span><br><span class="line">        System.out.println(<span class="string">"除法操作："</span>+bigInstance.divide(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>)));</span><br><span class="line">         <span class="comment">//取该大数字 除以 3 的商</span></span><br><span class="line">        System.out.println(<span class="string">"取商："</span>+bigInstance.divideAndReainder(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>))[<span class="number">0</span>]);</span><br><span class="line">          <span class="comment">//取该大数字 除以 3 的余数</span></span><br><span class="line">        System.out.println(<span class="string">"取商："</span>+bigInstance.divideAndReainder(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>))[<span class="number">1</span>]);</span><br><span class="line">         <span class="comment">//取该大数字的 2 次方</span></span><br><span class="line">        System.out.println(<span class="string">"做 2 次方的操作："</span>+bigInstance.pow(<span class="number">2</span>));</span><br><span class="line">         <span class="comment">//取该大数字的相反数</span></span><br><span class="line">        System.out.println(<span class="string">"取相反数操作："</span>+bigInstance.negate());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="BigDecimal"><a href="#BigDecimal" class="headerlink" title="BigDecimal"></a>BigDecimal</h3><p>一般的 float 型和 double 型数据只可以用来做科学计算或工程计算，但由于在商业计算中要求数字精度比较高，所以要用到 java.math.BigDecimal 类。BigDecimal 类支持任何精度的定点数，可以用它来精确计算货币值。</p>
<h4 id="两个常用的构造方法"><a href="#两个常用的构造方法" class="headerlink" title="两个常用的构造方法"></a>两个常用的构造方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">publilc <span class="title">BigDecimal</span><span class="params">(<span class="keyword">double</span> val)</span></span>;<span class="comment">//将双精度型转换为 BigDecimal 类型</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">BigDecimal</span><span class="params">(String val)</span></span>;<span class="comment">//将字符串转换为 BigDecimal 类型</span></span><br></pre></td></tr></table></figure>
<p><strong>例子：高精度大小运算操作</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.math.BigDecimal;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BigDecimalDemo</span></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> location = <span class="number">10</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">        加法运算</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> BigDecimal <span class="title">add</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2)</span></span>&#123;</span><br><span class="line">        BigDecimal b1 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value1));</span><br><span class="line">        BigDecimal b2 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value2));</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> b1.add(b2);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">        减法运算</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> BigDecimal <span class="title">add</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2)</span></span>&#123;</span><br><span class="line">        BigDecimal b1 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value1));</span><br><span class="line">        BigDecimal b2 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value2));</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> b1.subtract(b2);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">        乘法运算</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> BigDecimal <span class="title">add</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2)</span></span>&#123;</span><br><span class="line">        BigDecimal b1 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value1));</span><br><span class="line">        BigDecimal b2 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value2));</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> b1.multiply(b2);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">        除法运算</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> BigDecimal <span class="title">add</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> div(value1, value2, location);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//定义除法方法，参数分别为除数与被除数以及商小数点后的余数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">div</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2, <span class="keyword">int</span> b)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (b &lt;  <span class="number">0</span>) &#123;</span><br><span class="line">            System.out.printn(<span class="string">"b 值必须大于等于 0 "</span>);</span><br><span class="line">        &#125;</span><br><span class="line">         BigDecimal b1 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value1));</span><br><span class="line">        BigDecimal b2 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value2));</span><br><span class="line">        <span class="keyword">return</span> b1.divide(b2, b, BigDecimal.ROUND_HALF_UP);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="面试题"><a href="#面试题" class="headerlink" title="面试题"></a>面试题</h3><h4 id="下列程序的输出结果是什么？"><a href="#下列程序的输出结果是什么？" class="headerlink" title="下列程序的输出结果是什么？"></a>下列程序的输出结果是什么？</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.＊;</span><br><span class="line"><span class="keyword">import</span> java.math.BigInteger;</span><br><span class="line"><span class="keyword">import</span> java.lang.＊;</span><br><span class="line"><span class="keyword">public</span>   <span class="class"><span class="keyword">class</span>   <span class="title">Test</span>   </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span>   <span class="keyword">static</span>   <span class="keyword">void</span>   <span class="title">main</span><span class="params">(String[]   args)</span>  <span class="keyword">throws</span> NumberFormatException </span>&#123;</span><br><span class="line">      BigInteger   one=<span class="keyword">new</span>     BigInteger(<span class="string">"1"</span>);</span><br><span class="line">      BigInteger   two=<span class="keyword">new</span>     BigInteger(<span class="string">"2"</span>);</span><br><span class="line">      BigInteger   three=<span class="keyword">new</span>   BigInteger(<span class="string">"3"</span>);</span><br><span class="line">      BigInteger   sum=<span class="keyword">new</span>     BigInteger(<span class="string">"0"</span>);</span><br><span class="line">      sum.add(one);</span><br><span class="line">      sum.add(two);</span><br><span class="line">      sum.add(three);</span><br><span class="line">      System.out.println(sum.toString());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>A. 6</li>
<li>B. 0</li>
<li>C. 1 </li>
<li>D. 3</li>
</ul>
<p><strong>解析：</strong> 本题考的是Java中的大数类。<br>（1）BigInteger属于java.math.BigInteger，因此，在每次使用前都要import（输入）这个类，否则会提示找不到提示符。<br>（2）BigInteger构造方法有很多，如：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">BigInteger(String val)</span><br><span class="line"><span class="comment">//将BigInteger的十进制字符串表示形式转换为BigInteger</span></span><br><span class="line">BigInteger(String val，<span class="keyword">int</span> radix)</span><br><span class="line"><span class="comment">//将指定基数的BigInteger的字符串表示形式转换为BigInteger</span></span><br><span class="line">如要将<span class="keyword">int</span>型的<span class="number">2</span>转换为BigInteger型，要写为：</span><br><span class="line">BigInteger two=<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>); <span class="comment">//注意2的双引号不能省略</span></span><br></pre></td></tr></table></figure></p>
<p>（3）BigInteger类模拟了所有的int型数学操作，如add()==+，divide()==-等，但注意进行数学运算时，不能直接使用数学运算符进行运算，必须使用其内部方法，而且其操作数也必须为BigInteger型。<br>如：two.add(2)就是一种错误的操作，因为2没有改为BigInteger型。<br>对本题而言，sum返回一个biginteger的值并不改变原来的变量，所以sum的值始终为0。如果想得到6的结果，必须显式地返回，代码如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.＊;</span><br><span class="line"><span class="keyword">import</span> java.math.BigInteger;</span><br><span class="line"><span class="keyword">import</span> java.lang.＊;</span><br><span class="line"><span class="keyword">public</span>   <span class="class"><span class="keyword">class</span>   <span class="title">Test</span>   </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span>   <span class="keyword">static</span>   <span class="keyword">void</span>   <span class="title">main</span><span class="params">(String[]   args)</span>  <span class="keyword">throws</span> NumberFormatException </span>&#123;</span><br><span class="line">      BigInteger   one=<span class="keyword">new</span>     BigInteger(<span class="string">"1"</span>);</span><br><span class="line">      BigInteger   two=<span class="keyword">new</span>     BigInteger(<span class="string">"2"</span>);</span><br><span class="line">      BigInteger   three=<span class="keyword">new</span>   BigInteger(<span class="string">"3"</span>);</span><br><span class="line">      BigInteger   sum=<span class="keyword">new</span>     BigInteger(<span class="string">"0"</span>);</span><br><span class="line">      sum = sum.add(one);</span><br><span class="line">      sum = sum.add(two);</span><br><span class="line">      sum = sum.add(three);</span><br><span class="line">      System.out.println(sum.toString());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>答案：</strong> B</p>

  	</div>
	  
	  <div class="article-tags tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
		
	
		<div class="art-item-footer">
				
					<span class="art-item-left"><i class="icon icon-chevron-thin-left"></i>prev：<a href="/2019/01/16/Java-类的继承/" rel="prev"  title="Java 类的继承">
						Java 类的继承 
					</a></span>
				
				
					<span class="art-item-right">next：<a href="/2019/01/14/Java-随机数/" rel="next"  title="Java 随机数">
						Java 随机数
					</a><i class="icon icon-chevron-thin-right"></i></span>
				
		</div>
	
	</section>
	
</article>
<script>
	window.subData = {
		title: 'Java 大数字运算',
		tools: true
	}
</script>

      </div>
      <aside class='l_side'>
        
  <section class='m_widget links'>
<div class='header'>Links</div>
<div class='content'>
    <ul class="entry">
    
        <li><a class="flat-box" target="_blank" href="https://iiong.com/">
            <div class='name'>淮城一只猫</div>
        </a></li>
    
    </ul>
</div>
</section>

  <section class='m_widget categories'>
<div class='header'>Categories</div>
<div class='content'>
    
    <ul class="entry">
    
        <li><a class="flat-box" href="/categories/Android基础/"><div class='name'>Android基础</div><div class='badget'>1</div></a></li>
    
    </ul>
    
</div>
</section>

  
<div class="m_widget tagcloud">
    <div class="header">Tags</div>
    <div class='content'>
        <a href="/tags/Android/" style="font-size: 14px; color: #808080">Android</a> <a href="/tags/HTML/" style="font-size: 14px; color: #808080">HTML</a> <a href="/tags/Java/" style="font-size: 20px; color: #000">Java</a> <a href="/tags/Python/" style="font-size: 14px; color: #808080">Python</a>
    </div>
</div>



      </aside>
      <script>setLoadingBarProgress(60);</script>
    </div>
  </div>
  <footer id="footer" class="clearfix">

	<div class="social-wrapper">
  	
      
        <a href="https://github.com/stkevintan" class="social github"
          target="_blank" rel="external">
          <span class="icon icon-github"></span>
        </a>
      
        <a href="https://twitter.com/kevinsfork" class="social twitter"
          target="_blank" rel="external">
          <span class="icon icon-twitter"></span>
        </a>
      
        <a href="/atom.xml" class="social rss"
          target="_blank" rel="external">
          <span class="icon icon-rss"></span>
        </a>
      
    
  </div>
  
  <div>Theme <a href='https://github.com/stkevintan/hexo-theme-material-flow' class="codename">MaterialFlow</a> designed by <a href="http://keyin.me/" target="_blank">Kevin Tan</a>.</div>
  
</footer>


  <script>setLoadingBarProgress(80);</script>
  

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src='//cdn.bootcss.com/node-waves/0.7.5/waves.min.js'></script>
<script src="//cdn.bootcss.com/scrollReveal.js/3.3.2/scrollreveal.min.js"></script>
<script src="/js/jquery.fitvids.js"></script>
<script>
	var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
	var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
	var ALGOLIA_API_KEY = "";
	var ALGOLIA_APP_ID = "";
	var ALGOLIA_INDEX_NAME = "";
  var AZURE_SERVICE_NAME = "";
  var AZURE_INDEX_NAME = "";
  var AZURE_QUERY_KEY = "";
  var BAIDU_API_ID = "";
  var SEARCH_SERVICE = "hexo";
  var ROOT = "/"||"/";
  if(!ROOT.endsWith('/'))ROOT += '/';
</script>
<script src="/js/search.js"></script>
<script src="/js/app.js"></script>


  <script>setLoadingBarProgress(100);</script>
</body>
</html>
